#ifndef EVENTLOOP_POLLER_H
#define EVENTLOOP_POLLER_H

#include "IOchannelProxyFwd.h"
#include "utility/chrono.h"
#include "utility/Noncopyable.h"

#include <memory>

class Eventloop;

/// @class owned by independent IO thread
/// @brief IO multiplexing wrapper
class Poller {
public:
    static std::unique_ptr<Poller> DEFAULT(Eventloop &loop);

    explicit Poller(const Eventloop &loop) : _loop{loop} {};
    virtual ~Poller();

    virtual bool has(const IOchannelProxy &channel) const = 0;

    /// @param timeout ms
    virtual Utility::timepoint poll(int timeout , IOchannelProxyRefs &activeChannels) = 0;
    virtual void remove(IOchannelProxy &channel) = 0;
    virtual void update(IOchannelProxy &channel) = 0;

protected:
    virtual void _fill_active_channels(
        int nfds, IOchannelProxyRefs &activeChannels) const = 0;
    Utility::timepoint _polling_nfds(int fds, IOchannelProxyRefs &activeChannels) const;

    const Eventloop &_loop;

private:
    NONCOPYABLE(Poller);
};


#endif